// Listing 19.2 The Implementation of the Template MyArray
#include "MyArray.h"
#include <iostream>
#include <type_traits>
//const int DefaultSize = 10;

// declare a simple Animal class so that we can
// create an array of animals
class Animal {
public:
  Animal(int);
  Animal();
  ~Animal();
  int GetWeight() const { return itsWeight; }
  void Display() const { std::cout << itsWeight; }
private:
  int itsWeight;
};

Animal::Animal(int weight) : itsWeight(weight) {}

// template <class T> class MyArray {
// public:
//   MyArray(int itsSize) DefaultSize;
//   MyArray(const MyArray &rhs);
//   ~MyArray() { delete[] pType }

// private:
//   T *pType;
//   int itsSize;
// };

// implement the Constructor
// should set a default value
// copy constructor
template <class T> MyArray<T>::MyArray(int size) : itsSize(size) {
  pType = new T[size];
  // the constructors of the type you are creating
  // should set a default value
}

// copy constructor
template <class T> MyArray<T>::MyArray(const MyArray &rhs) {
  itsSize = rhs.GetSize();
  pType = new T[itsSize];
  for (int i = 0; i < itsSize; i++) {
    pType[i] = rhs[i];
  }
}

// operator=
template <class T> MyArray<T>& MyArray<T>::operator=(const MyArray &rhs) {
  if (this == &rhs) {
    return *this;
  }
  delete[] pType;
  itsSize = rhs.GetSize();
  pType = new T[itsSize];
  for (int i = 0; i < itsSize; i++) {
    pType[i] = rhs[i];
  }
  return *this;
}

// driver program
int main(void) {
  MyArray<int> theArray;
  MyArray<Animal> theZoo;
  Animal *pAnmal;

  // fill tjhe arrays
  for (int i = 0; i < theArray.GetSize(); i++) {
    theArray[i] = i * 2;
    pAnmal = new Animal(i * 3);
    theZoo[i] = *pAnmal;
    delete pAnmal;
  }

  // print the contents of the arrays
  for(int j = 0; j < theArray.GetSize(); j++) {
    std::cout << "theArray[" << j << "]:\t";
    std::cout << theArray[j] << "\t\t";
    std::cout << "theZoo[" << j << "]:\t";
    theZoo[j].Display();
    std::cout << std::endl;
  }
}